package com.yoomiito.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yoomiito.app.utils.AllUtils;

public abstract class BaseDialog extends Dialog {

    public Context mContext;
    private int mGravity = Gravity.CENTER;
    private double mWidthPercent = 0.75;
    private double mHeightPercent = -1;
    private boolean isWrapContent = false;
    private boolean isFullScreen = false;
    private int marginTop = 0;
    public BaseDialog(@NonNull Context context) {
        this(context, 0);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setView());
        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (isWrapContent) {
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        } else if (isFullScreen) {
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        } else {
            int width = getScreenWidth();
            int height = getScreenHeight();
            if (mWidthPercent != -1) {
                lp.width = (int) ((width >= height ? height : width) * mWidthPercent);
            }
            if (mHeightPercent != -1) {
                lp.height = (int) ((width >= height ? width : height) * mHeightPercent);
            }
        }
        lp.gravity = mGravity;
        mWindow.setAttributes(lp);
        initView();
    }

    public void initView() {
    }

    public void show() {
        if (!isShowing() && !AllUtils.isApplicationInBackground(getContext())) {
            super.show();
        }
    }

    public void setXY(int x,int y){
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.y = y;
        lp.x = x;
        getWindow().setAttributes(lp);
    }

    public void dismiss() {
        super.dismiss();
    }

    public void setmGravity(int mGravity) {
        this.mGravity = mGravity;
    }

    public void setWidthPercent(double mWidthPercent) {
        this.mWidthPercent = mWidthPercent;
    }

    public void setmHeightPercent(double mHeightPercent) {
        this.mHeightPercent = mHeightPercent;
    }

    public void setWrapContent() {
        isWrapContent = true;
    }

    public void setFullScreen(boolean fullScreen) {
        isFullScreen = fullScreen;
    }

    public abstract View setView();

    public void setMarginTop(int marginTop) {
        this.marginTop = marginTop;
    }

    @Override
    public void onBackPressed() {
        dismiss();
    }

    private int getScreenWidth() {
        Resources resources = mContext.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        return displayMetrics.widthPixels;
    }

    private int getScreenHeight() {
        Resources resources = mContext.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        return displayMetrics.heightPixels;
    }
}
